//package com.lee.gateway.exception;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.lee.common.model.AppResult;
//import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * 网关全局异常处理
// * ErrorWebExceptionHandler 主要用于 网关层面 的应用场景。
// * 对于下游微服务，异常处理一般通过 @ControllerAdvice 和 @ExceptionHandler 注解来实现。
// * @author: ChickenWing
// * @date: 2023/10/28
// */
//@Component
//public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
//
//    private ObjectMapper objectMapper = new ObjectMapper();
//
//    @Override
//    public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
//        ServerHttpRequest request = serverWebExchange.getRequest();
//        ServerHttpResponse response = serverWebExchange.getResponse();
//        Integer code = 200;
//        String message = "";
//        if (throwable instanceof SaTokenException) {
//            code = 401;
//            message = "用户无权限";
//            throwable.printStackTrace();
//        } else {
//            code = 500;
//            message = "系统繁忙";
//            throwable.printStackTrace();
//        }
//        AppResult result = AppResult.failed(code, message);
//        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
//        return response.writeWith(Mono.fromSupplier(() -> {
//            DataBufferFactory dataBufferFactory = response.bufferFactory();
//            byte[] bytes = null;
//            try {
//                bytes = objectMapper.writeValueAsBytes(result);
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//            return dataBufferFactory.wrap(bytes);
//        }));
//    }
//
//}
